package cn.openea.perpartner.cms.web.controller;

import cn.openea.perpartner.cms.factory.UserContext;
import cn.openea.perpartner.common.auth.entity.LoginBody;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import java.util.Arrays;
import java.util.HashMap;

@Controller
public class AdminRouteController {


    @RequestMapping("/Admin")
    public String admin(Model model, HttpServletRequest request) {
        LoginBody loginUser = UserContext.getLoginUser();
        String userRole = UserContext.getUserRole();
        model.addAttribute("loginUser", loginUser);
        model.addAttribute("userRole", userRole);
        String final_tab = getTabByRole(userRole,"RoleIndex");
        model.addAttribute("Tab", final_tab);
        return "index";
    }

    @RequestMapping("/Admin/{tab}")
    public String admin(Model model, @PathVariable(name = "tab") String tab, HttpServletRequest request) {
        LoginBody loginUser = UserContext.getLoginUser();
        String userRole = UserContext.getUserRole();
        String final_tab = getTabByRole(userRole,tab);
        model.addAttribute("loginUser", loginUser);
        model.addAttribute("userRole", userRole);
        model.addAttribute("Tab", final_tab);
        return "index";
    }

    @RequestMapping("/Admin/{folder}/{tab}")
    public String AdminFolder(Model model, @PathVariable(name = "folder") String folder, @PathVariable(name = "tab") String tab, HttpServletRequest request) {
        LoginBody loginUser = UserContext.getLoginUser();
        System.out.println("---------");
        System.out.println(loginUser);
        System.out.println("---------");
        String userRole = UserContext.getUserRole();
        String position = folder+"/"+tab;
        String final_tab = getTabByRole(userRole,position);
        model.addAttribute("loginUser", loginUser);
        model.addAttribute("userRole", userRole);
        model.addAttribute("Tab", final_tab);
        return "index";
    }

    @RequestMapping("/Fragment/{folder}/{tab}")
    public String TabFolder(Model model, @PathVariable(name = "folder") String folder, @PathVariable(name = "tab") String tab, HttpServletRequest request) {
        LoginBody loginUser = UserContext.getLoginUser();
        String userRole = UserContext.getUserRole();
        String position = folder+"/"+tab;
        String final_tab = getTabByRole(userRole,position);
        model.addAttribute("loginUser", loginUser);
        model.addAttribute("userRole", userRole);
        return final_tab;
    }

    /**
     * 根据角色获取
     * @param userRole
     * @param tab
     * @return
     */
    private String getTabByRole(String userRole,String tab){
        HashMap<String,String[]> RoleRouteMap = new HashMap<String,String[]>();
        String[] commonPages = {"Brand/BrandMerchant", "Brand/EditBrandMerchant", "Brand/BrandDetails", "Brand/BrandDetailsReadonly", "Commodity/CommodityManagement", "Commodity/CommodityDetails", "Brand/CollectionBrand", "GroupPurchase/CommunityLeader", "GroupPurchase/CollectionGroupPurchase", "GroupPurchase/EditGroupPurchaseInfo"};//普通角色
        //上游销售
        String[] upstreamSalesPages = {"Brand/BrandMerchant", "Brand/EditBrandMerchant", "Brand/BrandDetails", "Brand/BrandDetailsReadonly", "Commodity/CommodityManagement", "Commodity/CommodityDetails", "Brand/CollectionBrand", "GroupPurchase/CommunityLeader", "GroupPurchase/CollectionGroupPurchase", "GroupPurchase/EditGroupPurchaseInfo"};
        //下游销售
        String[] downstreamSalesPages = {"Brand/BrandMerchant", "Brand/EditBrandMerchant", "Brand/BrandDetails", "Brand/BrandDetailsReadonly", "Commodity/CommodityManagement", "Commodity/CommodityDetails", "Brand/CollectionBrand", "GroupPurchase/CommunityLeader", "GroupPurchase/CollectionGroupPurchase", "GroupPurchase/EditGroupPurchaseInfo"};
        String[] managePages = {"Brand/BrandMerchant", "Commodity/CommodityManagement", "GroupPurchase/CommunityLeader", "Sales/SalesManagement", "Sales/AssistantManagement"};//管理员角色
        String[] deptManagePages = {"Brand/BrandMerchant", "Brand/EditBrandMerchant", "Brand/BrandDetails", "Brand/BrandDetailsReadonly", "Commodity/CommodityManagement", "Commodity/CommodityDetails", "GroupPurchase/CommunityLeader", "GroupPurchase/EditGroupPurchaseInfo", "Sales/SalesManagement", "Sales/EditAssistantInfo", "Sales/EditSalesInfo", "Sales/AssistantManagement", "Tab/CollectionBrand", "Tab/CollectionGroupPurchase", "Tab/AssistantRelatedSales", "Tab/BrandDetailsReadonly", "Sales/SalesInfoReadonly", "Tab/SalesInfoReadonly"};//部门经理角色
        String[] busineManagePages = {"Brand/BrandMerchant", "Commodity/CommodityManagement", "GroupPurchase/CommunityLeader", "Sales/SalesManagement", "Sales/AssistantManagement"};//业务总监角色
        String[] salesManagePages = {"Brand/BrandMerchant", "Commodity/CommodityManagement", "Commodity/CommodityDetails", "Sales/SalesManagement", "Sales/EditSalesInfo", "Brand/BrandDetails", "Brand/BrandDetailsReadonly", "Tab/CollectionBrand", "Tab/CollectionGroupPurchase"};//销售助理角色
        String[] taobaoManagePages = {"Commodity/CommodityManagement", "Commodity/CommodityDetails", "Brand/BrandDetailsReadonly"};//淘宝运营角色
        String[] priceClerkPages = {"Commodity/CommodityManagement", "Commodity/CommodityDetails", "Brand/BrandDetailsReadonly"};//商品定价员角色
        RoleRouteMap.put("common",commonPages);
        RoleRouteMap.put("upstreamSales",upstreamSalesPages);
        RoleRouteMap.put("downstreamSales",downstreamSalesPages);
        RoleRouteMap.put("manage",managePages);
        RoleRouteMap.put("deptManage",deptManagePages);
        RoleRouteMap.put("busineManage",busineManagePages);
        RoleRouteMap.put("salesManage",salesManagePages);
        RoleRouteMap.put("taobaoManage",taobaoManagePages);
        RoleRouteMap.put("priceClerk",priceClerkPages);
        String[] RoleArr = userRole.split(",");
        String indexpage = "";
        if(RoleArr!=null && RoleArr.length>0){
            for(String role : RoleArr){
                String[] tabs = RoleRouteMap.get(role);
                if(tabs!=null && tabs.length>0){
                    boolean flag = Arrays.asList(tabs).contains(tab);
                    if (flag){
                        return tab;
                    }else {
                        indexpage = tabs[0];
                    }
                }else{
                    return "common/error";
                }
            }
        }else{
            return "common/error";
        }
        return indexpage;
    }


}
